package cn.smile.business.log.impl;

import cn.smile.bean.entity.log.IpQueryInfo;
import cn.smile.bean.form.log.ip.IpInfoAddForm;
import cn.smile.business.log.IIpInfoService;
import cn.smile.commons.constant.NumberConstant;
import cn.smile.commons.domain.BaseServiceImpl;
import cn.smile.commons.exceptions.BusinessException;
import cn.smile.commons.response.MyResponseCode;
import cn.smile.repository.log.IpQueryInfoMapper;
import cn.smile.utils.BeanCopy;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jthinking.common.util.ip.IPInfo;
import com.jthinking.common.util.ip.IPInfoUtils;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author smile-jt
 */
@Slf4j
@Service
public class IpInfoServiceImpl extends BaseServiceImpl<IpQueryInfoMapper, IpQueryInfo> implements IIpInfoService {

    @PostConstruct
    public void init() {
        log.info("初始化IP Info Utils 工具类 Start");
        IPInfoUtils.init();
        log.info("初始化IP Info Utils 工具类 End");
    }


    @Override
    public Boolean checkIpAdd(String ip) {
        log.info("[IpInfoServiceImpl].[checkIpAdd] ------> 检查IP信息 Start, ip = {}", ip);
        boolean result;
        IpQueryInfo info = baseMapper.selectOne(new QueryWrapper<IpQueryInfo>().lambda().eq(IpQueryInfo::getDeleted, Boolean.FALSE).eq(IpQueryInfo::getIp, ip));
        if (ObjectUtils.isEmpty(info)) {
            IPInfo ipInfo = IPInfoUtils.getIpInfo(ip);
            IpInfoAddForm add = BeanCopy.copyBean(ipInfo, IpInfoAddForm.class);
            add.setIp(ip);
            add.setLng(BigDecimal.valueOf(ipInfo.getLng()));
            add.setLat(BigDecimal.valueOf(ipInfo.getLat()));
            add.setOverseas(ipInfo.isOverseas());
            this.addInfo(add);
            result = ipInfo.isOverseas();
        } else {
            result = info.getOverseas();
        }
        log.info("[IpInfoServiceImpl].[checkIpAdd] ------> 检查IP信息 End, result = {}", result);
        return result;
    }

    @Override
    public Boolean addInfo(IpInfoAddForm form) {
        log.info("[IpInfoServiceImpl].[addInfo] ------> 新增IP查询记录 Start, form = {}", JSON.toJSONString(form));
        if (this.checkIpExist(form.getIp())) {
            throw new BusinessException(MyResponseCode.DATA_EXIST_ERROR);
        }
        IpQueryInfo entity = BeanCopy.copyBean(form, IpQueryInfo.class);
        super.addBaseCol(entity);
        int insertNum = NumberConstant.ZERO;
        try {
            insertNum = baseMapper.insert(entity);
        } catch (Exception e){
            log.error("IP 地址 [{}] 插入数据库失败, 可能已经存在!", form.getIp());
        }
        log.info("[IpInfoServiceImpl].[addInfo] ------> 新增IP查询记录 End, insertNum = {}", insertNum);
        return insertNum > NumberConstant.ZERO;
    }

    @Override
    public Boolean checkIpExist(String ip) {
        log.info("[IpInfoServiceImpl].[checkIpExist] ------> 检查IP是否存在 Start, ip = {}", ip);
        List<IpQueryInfo> infos = baseMapper.selectList(new QueryWrapper<IpQueryInfo>().lambda().eq(IpQueryInfo::getIp, ip).eq(IpQueryInfo::getDeleted, Boolean.FALSE));
        boolean result = !CollectionUtils.isEmpty(infos);
        log.info("[IpInfoServiceImpl].[checkIpExist] ------> 检查IP是否存在 End, result = {}", result);
        return result;
    }
}
